package com.example.oasystem.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.oasystem.dao.dept.AoaDeptDao;
import com.example.oasystem.dao.login.AoaUserDao;
import com.example.oasystem.dao.process.AoaHolidayDao;
import com.example.oasystem.dao.process.AoaProcessListDaoOfJian;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.process.AoaHoliday;
import com.example.oasystem.entity.process.AoaProcessListOfJian;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class AoaUserServiceImpl implements AoaUserService {

    @Autowired
    private AoaUserDao aoaUserDao;
    @Autowired
    private AoaDeptDao aoaDeptDao;
    @Autowired
    private AoaHolidayDao aoaHolidayDao;
    @Autowired
    private AoaProcessListDaoOfJian aoaProcessListDaoOfJian;

    @Override
    public AoaUser getAoaUser(String userName, String password) {

        return aoaUserDao.getAoaUser(userName,password);
    }

    @Override
    public List<AoaUser> getAoaUserList() {
        return aoaUserDao.getAoaUserList();
    }

    @Override
    public int updateAoaUserHasPwd(AoaUser aoaUser) {
        return aoaUserDao.updateAoaUserHasPwd(aoaUser);
    }

    @Override
    public int updateAoaUserNoPwd(AoaUser aoaUser) {
        return aoaUserDao.updateAoaUserNoPwd(aoaUser);
    }

    @Override
    public int updateImgPathById(AoaUser aoaUser) {
        return aoaUserDao.updateImgPathById(aoaUser);
    }

    @Override
    public Long selectFartherIdByDeptAndPosition( Long deptId,Long roleId) {
        Long fartherId = 1L;
        if(deptId==1){
            //总经办
            if(roleId==3){
                //总经理
                fartherId = 2L;
            }
        }else {
            //其他部门
            //是否是部门经理
            if(roleId==4){
                fartherId = aoaDeptDao.selectById(1L).getDeptmanager();
            }else {
                fartherId = aoaDeptDao.selectById(deptId).getDeptmanager();
            }
        }

        return fartherId;
    }
    @Override
    public List<AoaUser> selectByDeptId(Long deptId, Page page) {
        System.out.println("page = " + page);
        return aoaUserDao.selectByDeptId(deptId,page);
    }

    @Override
    public int updateDeptAndPosition(Long changedeptid, Long positionid, Long userid) {
        return aoaUserDao.updateDeptAndPosition(changedeptid,positionid,userid);
    }

    @Override
    public int updatePositionId(long userId,long deptId) {
        return aoaUserDao.updatePositionId(userId,deptId);
    }

    @Override
    public List<AoaUser> getUser(long roleId) {
        return aoaUserDao.getUser(roleId);
    }

    @Override
    public List<AoaUser> selectUserDeptAndAttendsList(Long userId,String startime,String endtime,String baseKey,int number,int size) {
        return aoaUserDao.selectUserDeptAndAttendsList(userId,startime,endtime,baseKey,number,size);
    }


    @Override
    public List<AoaUser> selectUserDeptAndAttendsListByMonth(Long userId, String month,String baseKey,int number, int size) {
        return aoaUserDao.selectUserDeptAndAttendsListByMonth(userId,month,baseKey,number,size);
    }

    @Override
    public HashMap getUMap(Long userId, String month) {
        List<AoaUser> aoaUsers = aoaUserDao.selectUserDeptAndAttendsListByMonth(userId, month,"",0,0);
        HashMap<String, List<Integer>> uMap = new HashMap<>();
        for (AoaUser aoaUser : aoaUsers) {
            //获取迟到 早退 正常 信息
            List<Integer> resultList = new ArrayList<>();
            // 获取各种天数
            int normalDays = aoaUserDao.countDays(aoaUser.getUserId(),month,10);
            int lateDays = aoaUserDao.countDays(aoaUser.getUserId(),month,11);
            int leaveDays = aoaUserDao.countDays(aoaUser.getUserId(),month,12);

            resultList.add(normalDays);
            resultList.add(lateDays);
            resultList.add(leaveDays);
            resultList.add(0);
            resultList.add(0);
            resultList.add(0);
            uMap.put(aoaUser.getUserName(),resultList);
        }
        List<AoaUser> userList = aoaUserDao.selectUserProcessHoliAndEvec(userId, month);
        System.out.println("11111111111111111111111111111111111111111");
        for (AoaUser user : userList) {
            Long userId1 = user.getUserId();
            String userName = user.getUserName();
            if (uMap.containsKey(userName)){
                // 请假天数
                int holidays = 0;
                List<AoaProcessListOfJian> processHoliday = aoaProcessListDaoOfJian.selectList(new QueryWrapper<AoaProcessListOfJian>().eq("process_user_id", userId1)
                        .eq("status_id",25L).eq("type_name","请假申请")
                        .like("apply_time",month));
                for (AoaProcessListOfJian each : processHoliday) {
                    AoaHoliday holiday = aoaHolidayDao.selectOne(new QueryWrapper<AoaHoliday>().eq("pro_id", each.getProcessId()));
                    holidays += holiday.getLeaveDays();
                }
                // 出差天数
                int evecdays = 0;
                List<AoaProcessListOfJian> processEvecday = aoaProcessListDaoOfJian.selectList(new QueryWrapper<AoaProcessListOfJian>().eq("process_user_id", userId1)
                        .eq("status_id",25L).eq("type_name","出差申请")
                        .like("apply_time",month));
                for (AoaProcessListOfJian each : processEvecday) {
                    evecdays += each.getProcseeDays();
                }
                uMap.get(userName).set(3,holidays);
                uMap.get(userName).set(4,evecdays);
            }else {
                System.out.println("222222222222222222222222222222");
                // 请假天数
                int holidays = 0;
                List<AoaProcessListOfJian> processHoliday = aoaProcessListDaoOfJian.selectList(new QueryWrapper<AoaProcessListOfJian>().eq("process_user_id", userId1)
                        .eq("status_id",25L).eq("type_name","请假申请")
                        .like("apply_time",month));
                for (AoaProcessListOfJian each : processHoliday) {
                    AoaHoliday holiday = aoaHolidayDao.selectOne(new QueryWrapper<AoaHoliday>().eq("pro_id", each.getProcessId()));
                    holidays += holiday.getLeaveDays();
                }
                System.out.println("holidays = " + holidays);
                // 出差天数
                int evecdays = 0;
                List<AoaProcessListOfJian> processEvecday = aoaProcessListDaoOfJian.selectList(new QueryWrapper<AoaProcessListOfJian>().eq("process_user_id", userId1)
                        .eq("status_id",25L).eq("type_name","出差申请")
                        .like("apply_time",month));
                for (AoaProcessListOfJian each : processEvecday) {
                    evecdays += each.getProcseeDays();
                }
                //获取请假，出差
                List<Integer> holiAndEvecList = new ArrayList<>();
                holiAndEvecList.add(0);
                holiAndEvecList.add(0);
                holiAndEvecList.add(0);
                holiAndEvecList.add(holidays);
                holiAndEvecList.add(evecdays);
                holiAndEvecList.add(0);
                uMap.put(userName,holiAndEvecList);
                System.out.println("uMap = " + uMap);
            }
        }
        System.out.println("uMap = " + uMap);
        return uMap;
    }
    @Override
    public PageInfo<AoaUser> selectAllDeptAndPosition(int pageNum, int pageSize, String baseKey) {
        PageHelper.startPage(pageNum, pageSize);
        List<AoaUser> list = aoaUserDao.selectAllDeptAndPosition(baseKey);
        PageInfo<AoaUser> pageUsers = new PageInfo<>(list);
        return pageUsers;
    }
    public PageInfo<AoaUser> selectAllDeptAndPosition2(int pageNum, int pageSize, String baseKey,Long fatherid) {
        PageHelper.startPage(pageNum, pageSize);
        List<AoaUser> list = aoaUserDao.selectAllDeptAndPosition2(baseKey,fatherid);
        PageInfo<AoaUser> pageUsers = new PageInfo<>(list);
        return pageUsers;
    }
    @Override
    public List<AoaUser> subordinateUsers(AoaUser aoaUser) {
        System.out.println("uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu"+aoaUserDao.subordinateUsers(aoaUser));
        return aoaUserDao.subordinateUsers(aoaUser);
    }
    @Override
    public List<AoaUser> selectProcessShenuser(String... userName) {
        return aoaUserDao.selectProcessShenuser(userName);
    }

}
